package cn.lrwjz.androidguide.ui.exmple.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import cn.lrwjz.androidguide.R;
import cn.lrwjz.androidguide.ui.exmple.FragmentActivity;

public class DynamicFragment extends BaseFragment {
    public static final String ARG_CONTENT = "content";
    private String content;
    private OnFragmentInteractionListener listener;
    private TextView tvContent;

    public static DynamicFragment newInstance(String content) {
        DynamicFragment fragment = new DynamicFragment();
        Bundle args = new Bundle();
        args.putString(ARG_CONTENT, content);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            content = getArguments().getString(ARG_CONTENT);
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_dynamic, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        tvContent = view.findViewById(R.id.tv_content);
        tvContent.setText(content);

        Button btnSendToActivity = view.findViewById(R.id.btn_send_to_activity);
        btnSendToActivity.setOnClickListener(v -> {
            if (listener != null) {
                String message = "来自DynamicFragment的消息";
                // 向Activity发送数据
                listener.onFragmentInteraction(message);

                // 向StaticFragment发送数据：方式一
                Fragment fragment = ((FragmentActivity)getActivity()).getSupportFragmentManager().findFragmentById(R.id.fragment_static);
                // Fragment之间最好不要直接通信，应该通过Activity中转
//                if (fragment != null) {
//                    ((StaticFragment)fragment).updateContent(message);
//                }

                // 方式二
                StaticFragment staticFragment = ((FragmentActivity) getActivity()).getStaticFragment();
                if (staticFragment != null) {
                    staticFragment.updateContent(message);
                }
            }
        });
    }

    // 添加更新内容的方法
    public void updateContent() {
        if (tvContent != null) {
            assert getArguments() != null;
            content = getArguments().getString(ARG_CONTENT);
            tvContent.setText(content);
        }
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            listener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context + " 必须实现 OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        listener = null;
    }

    public interface OnFragmentInteractionListener {
        void onFragmentInteraction(String message);
    }
} 